Skip to content

feat: move nil check to call sites #658

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

SoulPancake
Copy link

@SoulPancake SoulPancake commented Mar 18, 2025

This addresses

// TODO(rbuckton): Move node != niltest to call sites
@rbuckton Can you please review this
I can undo the stylistic whitespace changes in the internal/ast/utilities.g
but I think they're for the better, LMK what you think

@SoulPancake SoulPancake changed the title feat: moving nil check to call sites of IsFunctionLikeDeclaration feat: move nil check to call sites - IsFunctionLikeDeclaration Mar 18, 2025
@SoulPancake SoulPancake changed the title feat: move nil check to call sites - IsFunctionLikeDeclaration feat: move nil check to call sites Mar 18, 2025
@jakebailey jakebailey requested a review from rbuckton March 18, 2025 18:31
@SoulPancake
Copy link
Author

@rbuckton Can you take a look at this?

@SaadiSave
Copy link

SaadiSave commented May 7, 2025

@SoulPancake Sorry for jumping on here for a slightly tangential issue, but I noticed that nil checks are inconsistent throughout the codebase. For example,

https://github.com/microsoft/typescript-go/blob/main/internal%2Fast%2Fast.go#L15-L31

type Visitor func(*Node) bool

func visit(v Visitor, node *Node) bool {
        if node != nil {
                return v(node)
        }
        return false
}

func visitNodes(v Visitor, nodes []*Node) bool {
        for _, node := range nodes {
                if v(node) {
                        return true
                }
        }
        return false
}

visit checks for nils, but the for loop doesn't check if node is nil. Do you have clear documentation on whether Visitors are supposed to handle nils? Because visit assumes they don't, and visitNodes assumes they do, which are mutually incompatible assumptions.

@SoulPancake
Copy link
Author

@SaadiSave Agreed, I will add the nil check in the loop

@SaadiSave
Copy link

@SoulPancake is there a lint to enforce nil checks?

@SoulPancake
Copy link
Author

Don't think so @SaadiSave

@rbuckton rbuckton removed their assignment May 25, 2025
@jakebailey jakebailey removed the request for review from rbuckton June 24, 2025 23:13
@jakebailey
Copy link
Member

This PR has gotten out of date as main changed. Could you update it, or close it if you don't plan on working on this anymore?

@SoulPancake
Copy link
Author

I will update it asap @jakebailey

@SoulPancake
Copy link
Author

@jakebailey Done, Can you take a look at this?

@jakebailey
Copy link
Member

I feel like most of these are redundant; I would think we'd only want ones that correspond to undefined checks in the original code.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
@Copilot Copilot AI review requested due to automatic review settings August 15, 2025 15:35
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR addresses a TODO comment by moving nil checks from the IsFunctionLike and IsFunctionLikeDeclaration utility functions to their call sites, improving code clarity and potentially performance by avoiding redundant nil checks.

Key changes:

  • Removed internal nil checks from IsFunctionLike and IsFunctionLikeDeclaration functions
  • Added explicit nil checks at all call sites where these functions are used
  • Updated function documentation to clarify the nil check requirement

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
internal/ast/utilities.go Removed nil checks from IsFunctionLike and IsFunctionLikeDeclaration, added documentation comments
internal/checker/checker.go Added nil checks before calling IsFunctionLike and IsFunctionLikeDeclaration functions
internal/checker/grammarchecks.go Added nil checks before calling IsFunctionLikeDeclaration and IsFunctionLike functions
internal/checker/flow.go Added nil check before calling IsFunctionLike function
internal/binder/binder.go Added nil check before calling IsFunctionLike function

@@ -306,7 +306,7 @@ func (c *Checker) checkGrammarModifiers(node *ast.Node /*Union[HasModifiers, Has
parent := node.Parent

if node.Kind == ast.KindTypeParameter {
if !(ast.IsFunctionLikeDeclaration(parent) || ast.IsClassLike(parent) ||
if !((parent != nil && ast.IsFunctionLikeDeclaration(parent)) || ast.IsClassLike(parent) ||
Copy link
Preview

Copilot AI Aug 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The nil check is only applied to the first condition in the OR expression. The call to ast.IsClassLike(parent) and other functions on subsequent lines also need nil checks for parent.

Copilot uses AI. Check for mistakes.

@@ -2080,7 +2080,7 @@ func (c *Checker) checkGrammarStatementInAmbientContext(node *ast.Node) bool {
if node.Flags&ast.NodeFlagsAmbient != 0 {
// Find containing block which is either Block, ModuleBlock, SourceFile
links := c.nodeLinks.Get(node)
if !links.hasReportedStatementInAmbientContext && (ast.IsFunctionLike(node.Parent) || ast.IsAccessor(node.Parent)) {
if !links.hasReportedStatementInAmbientContext && (node.Parent != nil && ast.IsFunctionLike(node.Parent) || ast.IsAccessor(node.Parent)) {
Copy link
Preview

Copilot AI Aug 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The nil check is only applied to the first condition in the OR expression. The call to ast.IsAccessor(node.Parent) also needs a nil check for node.Parent.

Suggested change
if !links.hasReportedStatementInAmbientContext && (node.Parent != nil && ast.IsFunctionLike(node.Parent) || ast.IsAccessor(node.Parent)) {
if !links.hasReportedStatementInAmbientContext && node.Parent != nil && (ast.IsFunctionLike(node.Parent) || ast.IsAccessor(node.Parent)) {

Copilot uses AI. Check for mistakes.

@SoulPancake
Copy link
Author

@jakebailey I guess these aren't needed or helpful as you mentioned, do you suggest I remove the TODO comments and remove the changes?

@SoulPancake
Copy link
Author

Otherwise I am happy to close this PR @jakebailey

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants